package com.gedeshidai.shumadevelop.job;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gedeshidai.shumadevelop.controller.AdminController;
import com.gedeshidai.shumadevelop.model.entity.Product;
import com.gedeshidai.shumadevelop.model.enums.ProductCheckStateEnum;
import com.gedeshidai.shumadevelop.model.enums.ProductOnlineStateEnum;
import com.gedeshidai.shumadevelop.model.enums.ProductOwnerStateEnum;
import com.gedeshidai.shumadevelop.service.ProductService;
import com.gedeshidai.shumadevelop.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


//@Slf4j
//@Component
public class AutoJob {
    @Resource
    public ProductService productService;
    @Resource
    public UserService userService;
    @Scheduled(fixedRate = 60000)
    public void autoOnline()  {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("product_owner_state", ProductOwnerStateEnum.SUBMITTED_CHECK);
        productQueryWrapper.eq("product_check_state", ProductCheckStateEnum.PASS_CHECK);
        productQueryWrapper.eq("product_online_state", ProductOnlineStateEnum.NO_ONLINE);
        List<Product> list = productService.list(productQueryWrapper);
        List<Long> onlineList = list.stream().map(Product::getProductId).collect(Collectors.toList());
        userService.setOnLine(onlineList);
    }
    @Scheduled(fixedRate = 60000)
    public void autoOffline()  {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("product_owner_state", ProductOwnerStateEnum.SUBMITTED_CHECK);
        productQueryWrapper.eq("product_check_state", ProductCheckStateEnum.PASS_CHECK);
        productQueryWrapper.eq("product_online_state", ProductOnlineStateEnum.ONLINE);
        List<Product> list = productService.list(productQueryWrapper);
        List<Long> offlineList = list.stream().map(Product::getProductId).collect(Collectors.toList());
        userService.setOffLine(offlineList);

    }
}
